import {mapGetters} from 'vuex';
import globalHelper from "@/utils/globalHelper";

export default {
    data() {
        return {
            filters: {
                detectorType: '',
                description: ''
            },
            detectorTypeOptions: [
                {id: '智能功率变送器', name: '智能功率变送器'},
                {id: '空压机', name: '空压机'},
                {id: '温湿度氧气传感器', name: '温湿度氧气传感器'}
            ],
            addForm: {},
            addFormVisible: false,
            editForm: {},
            editFormVisible: false,
            addFormRules: {
                description: [
                    {required: true, message: "请输入设备名称", trigger: "blur"}
                ],
                detectorType: [
                    {required: true, message: "请输入传感器类型", trigger: "blur"}
                ],
                tableName: [
                    {required: true, message: "请输入采集数据表名", trigger: "blur"}
                ],

            },
            editFormRules: {
                description: [
                    {required: true, message: "请输入设备名称", trigger: "blur"}
                ],
                detectorType: [
                    {required: true, message: "请输入传感器类型", trigger: "blur"}
                ],
                tableName: [
                    {required: true, message: "请输入采集数据表名", trigger: "blur"}
                ],
            },
        }
    },
    computed: {
        ...mapGetters([
            'detectorList',
            'detectorPage'
        ])
    },
    watch: {},
    components: {},
    mounted() {
        this.getAllDataOpt();
    },
    methods: {
        getAllDataOpt: function () {
            this.$store.dispatch('getDetectorList', {...this.filters});
        },

        handleCurrentChange(val) {
            this.detectorPage.page = val - 1;
            this.getAllDataOpt();
        },
        handleSizeChange(val) {
            this.detectorPage.page = 0;
            this.detectorPage.rows = val;
            this.getAllDataOpt();
        },
        handleSearch() {
            this.detectorPage.page = 0;
            this.getAllDataOpt();
        },
        tableOperate(typeOpt, row) {
            if (typeOpt === 'edit') {
                this.editForm = Object.assign({}, row);
                this.editFormVisible = true;
            } else if (typeOpt === 'add') {
                this.addForm = {};
                this.addFormVisible = true;
            } else if (typeOpt === 'del') {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$store.dispatch('deleteDetector', {id: row.id}).then((res) => {
                        this.getAllDataOpt();
                    });
                });
            }
        },
        addFormSubmit: function () {
            globalHelper.formValidate(this, 'addForm').then((valid) => {
                if (valid) {
                    let requestData = Object.assign({}, this.addForm);
                    this.$store.dispatch('addDetector', requestData).then((res) => {
                        this.$message({
                            type: 'success',
                            message: '创建设备成功'
                        });
                        this.getAllDataOpt();
                        this.addFormVisible = false;
                    });
                }
            });

        },
        editFormSubmit: function () {

            globalHelper.formValidate(this, 'addForm').then((valid) => {
                if (valid) {
                    let requestData = Object.assign({}, this.editForm);
                    this.$store.dispatch('editDetector', requestData).then((res) => {
                        this.$message({
                            type: 'success',
                            message: '修改设备成功'
                        });
                        this.getAllDataOpt();
                        this.editFormVisible = false;
                    });
                }
            });

        },
    }
}
